//
//  CollectGiftViewController.m
//  B项目
//
//  Created by lanou on 15/12/23.
//  Copyright © 2015年 tzf. All rights reserved.
//

#import "CollectGiftViewController.h"
#import "ChooseGiftCollectionViewCell.h"
#import "ChooseGift.h"
#import "WebViewController.h"
#import "DeleteCollectionViewCell.h"
@interface CollectGiftViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) NSMutableArray *modelArray;

@property (nonatomic,strong) UICollectionView *collectionView ;
@end

@implementation CollectGiftViewController
static bool ison = YES;


- (void)viewDidLoad {
           ison = YES;
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemCancel) target:self action:@selector(back)];
    NSData *data = [defaults objectForKey:@"gift"];
    self.modelArray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    self.title = @"收藏的礼物";
    
    
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowlayout.itemSize = CGSizeMake(kWidth / 2 - 20, 0.4 * kHeight);
    
    
     self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowlayout];
    
    
    
    self.collectionView.collectionViewLayout = flowlayout;
    flowlayout.headerReferenceSize = CGSizeMake(0, 40);
    
    self.collectionView.dataSource = self;
   self. collectionView.delegate = self;
    
    
    [self.collectionView registerClass:[ChooseGiftCollectionViewCell class] forCellWithReuseIdentifier:@"HP"];
    [self.collectionView registerClass:[DeleteCollectionViewCell class] forCellWithReuseIdentifier:@"MP"];
    [self.view addSubview:self.collectionView];

    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.91 alpha:1];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(change)];

    
}
- (void)change{
    if (ison == YES) {
        ison = NO;
        [self.collectionView reloadData];
    }else{
        ison = YES;
        [self.collectionView reloadData];
    }
}

- (void)back{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        if (ison == YES) {
            ChooseGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HP" forIndexPath:indexPath];
            
            ChooseGift *choose = self.modelArray[indexPath.row];
            cell.choose = self.modelArray[indexPath.row];
            cell.choose = choose;
            cell.backgroundColor = [UIColor whiteColor];

        return cell;

        
    }else{
         DeleteCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MP" forIndexPath:indexPath];
        ChooseGift *choose = self.modelArray[indexPath.row];
        cell1.choose = self.modelArray[indexPath.row];
        cell1.choose = choose;
        cell1.backgroundColor = [UIColor whiteColor];
        return cell1;
    }


}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (ison == YES) {
        WebViewController *webvc = [[WebViewController alloc]init];
        ChooseGift *choose = [[ChooseGift alloc]init];
        choose = self.modelArray[indexPath.row];
        webvc.userid = choose.ID;
        webvc.style = 100;
        webvc.type = 1;
        [webvc setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:webvc animated:YES];
    }else{

        [NSUserDefaults standardUserDefaults];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"gift"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.modelArray removeObject:self.modelArray[indexPath.row]];
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_modelArray];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:data forKey:@"gift"];
        [self.collectionView reloadData];
        
    }
   
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
